<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统中心</el-breadcrumb-item>
            <el-breadcrumb-item>admin管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">

            <el-button  type="primary" @click="dialogVisible = true">添加管理员</el-button>

            <el-table
                    :data="adminList"
                    style="width: 100%">
                <el-table-column
                        type="index"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="adminName"
                        label="管理员账号"
                        width="160">
                </el-table-column>
                <el-table-column
                        prop="adminStatus"
                        label="管理员状态"
                        width="180">
                    <template slot-scope="scope">
                        {{scope.row.adminStatus|adminStatusFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间"
                        width="180">
                    <template slot-scope="scope">
                        {{scope.row.createTime|createTimeFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="320">
                    <!--点击按钮 需要绑定@click的时间 例如 @click="add"-->
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="delById(scope.row.id)">锁定</el-button>
                        <el-button type="primary" size="mini" @click="openEditAdminPass(scope.row.id)">修改管理员密码</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-dialog
                title="添加管理员"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form ref="form" :model="adminForm" label-width="80px" >
                <el-form-item label="登录账号">
                    <el-input v-model="adminForm.userName"></el-input>
                </el-form-item>
                <el-form-item label="登录密码">
                    <el-input v-model="adminForm.userPass"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearAdmin">取 消</el-button>
                 <el-button type="primary" @click="addAdmin">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="修改管理员密码"
                :visible.sync="dialogVisible2"
                width="30%">
            <el-form ref="form" :model="adminForm" label-width="100px" >
                <el-form-item label="管理员新密码">
                    <el-input v-model="adminForm.adminPass"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearAdmin">取 消</el-button>
                 <el-button type="primary" @click="editAdminPass">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import moment from "moment";

    export default {
        name: "AdminList",
        data(){
            return{
                adminList:[],
                adminForm:{
                    userName : '',
                    userPass : '',
                    adminPass : ''
                },
                dialogVisible:false,
                dialogVisible2:false,
                adminid:null,
            }
        },
        methods:{

            //列表实现
            adminSel:function () {
                let params = new FormData();
                let token = window.sessionStorage.getItem("admin_token")
                console.log(token)
                params.append("token", token)
                
                this.$http.post('/admin/getAdminList', 
                    params
                ).then((res)=>{
                    this.adminList = res.data.result.admins
                })
            },

            //逻辑删除
            delById:function (id) {
                let params = new FormData();
                params.append("userId", id)
                
                this.$http.post(
                    '/admin/delAdminUser',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.adminSel();
                })
            },

            //添加管理员
            addAdmin:function () {
                let params = new FormData();
                params.append("userName", this.adminForm.userName)
                params.append("userPass", this.adminForm.userPass)
                
                this.$http.post(
                    'admin/addAdminUser',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearAdmin();
                })
            },

            //打开修改密码的dialog
            openEditAdminPass:function(id){
                this.adminForm.id=id;
                this.dialogVisible2=true;

            },

            //修改密码
            editAdminPass:function(id){
                this.dialogVisible2=false
                let params = new FormData();
                params.append("userId", this.adminForm.id)
                params.append("userPass", this.adminForm.adminPass)
                
                this.$http.post(
                    'admin/setAdminPass',
                    params
                ).then((res)=>{
                    if (res.data.code == 1000) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearAdmin()
                })
            },

            //清空表单
            clearAdmin:function(){
                this.dialogVisible=false
                this.dialogVisible2=false
                this.adminForm.id=null
                this.adminForm.adminPass=""
                this.adminForm.userName=""
                this.adminSel()
            }

        },
        filters:{
            adminStatusFit:function (val) {
                if (val == '0'){
                    return "正常"

                }
                else if (val == '1'){
                    return "锁定"
                }
            },
            createTimeFit:function (val) {
                return moment(val).format("YYYY-MM-DD hh:mm:ss")
            },
        },
        mounted() {
            this.adminSel();
        }

    }
</script>

<style scoped>


</style>

